﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using System.Resources;
using WebCrawler.ProcessingFiles.CategoryClasses;

namespace WebCrawler.ProcessingFiles
{
    public partial class ProcessProducts : System.Web.UI.Page
    {
        ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();
        NewEggClasses vendor = new NewEggClasses();

        Exception ex = new Exception();
        ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();
        public String errorPage = "ProcessProducts";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                Response.Write("<p class='URLS'/>");
                Response.Write("<div class='products' style='display:block'>");
                GetProductPages();
                Response.Write("</div>");


            }
            if (Request.QueryString.Count > 0)
            {
                try
                {
                    string dataString = Request.QueryString["dataString"];
                    string product = "";
                    string prodname = "";
                    string prodvalue = "";
                    string workingStr = "";

                    dataString = dataString.Replace(",", " # ");

                    char[] prodnum = new char[] { '%' };
                    string[] prodNum = dataString.Split(prodnum);
                    //foreach (var c in prodNum)
                    //{
                    product = prodNum[0].ToString();
                    dataString = prodNum[1].ToString();
                    //}

                    char[] separator = new char[] { '^' };
                    char[] TS = new char[] { '[' };
                    char[] TE = new char[] { ']' };
                    char[] S = new char[] { '~' };
                    char[] IS = new char[] { '{' };
                    char[] IE = new char[] { '}' };

                    dataString = dataString.TrimEnd(separator);
                    dataString = dataString.Replace("#", ",");
                    dataString = dataString.Replace("|nbsp;", "  ");

                    dataString = dataString.Replace("|gt;", "  ");
                    dataString = dataString.Replace("|", "  ");




                    string[] strSplitArr = dataString.Split(separator);
                    foreach (var c in strSplitArr)
                    {

                        workingStr = c;
                        workingStr = workingStr.TrimStart(TS);
                        workingStr = workingStr.TrimEnd(TE);

                        string[] itemList = workingStr.Split(S);

                        itemList[0] = itemList[0].TrimStart(IS).TrimEnd(IE);
                        itemList[1] = itemList[1].TrimStart(IS).TrimEnd(IE);

                        prodname = itemList[0].ToString().Trim();
                        prodvalue = itemList[1].ToString().Trim();




                        string Name = Server.HtmlEncode(prodname).Trim();
                        string Value = Server.HtmlEncode(prodvalue).Trim();
                        int ProductID = 0;
                        if (!string.IsNullOrEmpty(product) && Int32.TryParse(product, out ProductID))
                        {
                            ProductID = int.Parse(product);
                        }


                        if (Value.Length > 0)
                        {
                            ProcessProductData(ProductID, Name, Value);
                        }
                    }
                }
                catch (Exception ex)
                {


                    ProcessErrorText(ex);

                }
            }
        }

       
        protected void ProcessProductData(int productID, string prodname, string prodvalue)
        {
            try
            {
                var query = from pda in dc.ProductDatas
                            where pda.productDataID == productID && pda.productDataName.Trim() == prodname.Trim() && pda.productDataValue.Trim() == prodvalue.Trim()
                            select pda;

                if (query.Count() == 0)
                {

                    ProductData pd = new ProductData();
                    pd.productLinksID = productID;
                    pd.productDataName = prodname;
                    pd.productDataValue = prodvalue;

                    dc.ProductDatas.InsertOnSubmit(pd);
                    dc.SubmitChanges();
                }

                var qry = from pl in dc.ProductLinks where pl.productID == productID select pl;

                var item = qry.Single();

                item.process = 1;
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {


                ProcessErrorText(ex);

            }
                
        }

      
        protected void GetProductPages()
        {
            try
            {
                var query = from pd in dc.ProductLinks where pd.process == 0 select pd;

                if (query.Count() > 0)
                {
                    string strResult = "";
                    string CurrURL = query.FirstOrDefault().productURL.Replace("|", "&").Trim();
                    string productID = query.FirstOrDefault().productID.ToString();

                    WebResponse objResponse;
                    WebRequest objRequest = System.Net.HttpWebRequest.Create(CurrURL);

                    objResponse = objRequest.GetResponse();

                    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                    {
                        strResult = sr.ReadToEnd();
                        sr.Close();
                    }

                    strResult = strResult.Replace("\"", "");
                    strResult = strResult.Replace("#", "");
                    strResult = strResult.Replace("&", "|");
                    strResult = strResult.Remove(0, 15000);



                    Response.Write("<p class=CURRURL>");
                    Response.Write(productID);
                    Response.Write("</p>");
                    Response.Write(strResult);

                    query.FirstOrDefault().process = 2;
                    dc.SubmitChanges();

                }
                else
                {
                    var qry = from pd in dc.ProductLinks where pd.process == 2 select pd;

                    foreach (var item in qry)
                    {
                        item.process = 0;
                        dc.SubmitChanges();
                    }

                    GetNextPage(null, null);
                }
             }
            catch (Exception ex)
            {


                ProcessErrorText(ex);

            }

        }

        protected void ProcessErrorText(Exception ex)
        {

            StringBuilder errorText = new StringBuilder();

            if (ex != null)
            {
                if (ex.Source != null)
                {
                    errorText.Append(errorPage + " :source: " + ex.Source);
                }
                if (ex.Message != null)
                {
                    errorText.Append(errorPage + " :message: " + ex.Message);
                }
                if (ex.InnerException != null)
                {
                    errorText.Append(errorPage + " :innerexception: " + ex.InnerException);
                }
                if (ex.TargetSite != null)
                {
                    errorText.Append(errorPage + " :target: " + ex.TargetSite);
                }

            }
            else
            {
                errorText.Append("unknown error");
            }

            ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();

            WebCrawler wc = new WebCrawler();
            wc.webCrawlerErrorText = errorText.ToString();

            dce.WebCrawlers.InsertOnSubmit(wc);
            dce.SubmitChanges();
        }

        protected void GetNextPage(object sender, EventArgs e)
        {
            Response.Redirect("/ProcessingFiles/RedirectForm.aspx?Type=2&Name=Prod");
        }
    }
}